package com.heima.comment.service.impl;

import com.heima.comment.config.feign.UserFeign;
import com.heima.comment.dto.ApCommentDto;
import com.heima.comment.dto.CommentDto;
import com.heima.comment.dto.CommentLikeDto;
import com.heima.comment.dto.CommentSaveDto;
import com.heima.comment.entity.ApComment;
import com.heima.comment.entity.ApCommentLike;
import com.heima.comment.service.IApCommentService;
import com.heima.common.aliyun.GreenTextScan;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppThreadLocalUtil;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ApCommentServiceImpl implements IApCommentService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private GreenTextScan greenTextScan;




    @Override
    public ResponseResult save(CommentSaveDto dto) {
        Integer userId = AppThreadLocalUtil.get().getUserId();
        if (userId == 0)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        try {
            Map  map = greenTextScan.greenTextScan(dto.getContent());
            String suggestion = (String) map.get("suggestion");
            if(!suggestion.equals("pass"))
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ApComment apComment = new ApComment();
        BeanUtils.copyProperties(dto,apComment);

        ResponseResult byId = userFeign.findById(userId);
        Map data = (Map) byId.getData();
        apComment.setEntryId(dto.getArticleId());
        apComment.setImage((String) data.get("image"));
        apComment.setAuthorId(userId);
        apComment.setAuthorName((String) data.get("name"));
        apComment.setCreatedTime(new Date());
        apComment.setFlag(0);
        apComment.setLikes(0L);
        apComment.setReply(0);
        apComment.setType(0);
        mongoTemplate.insert(apComment);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult load(CommentDto dto) {

        Query query = new Query(Criteria.where("entryId").is(dto.getArticleId()));
        List<ApComment> apComments = mongoTemplate.find(query, ApComment.class);
        ArrayList<ApCommentDto> apCommentDtos = new ArrayList<>();
        for (ApComment apComment : apComments) {
            ApCommentDto apCommentDto = new ApCommentDto();
            BeanUtils.copyProperties(apComment,apCommentDto);
            apCommentDto.setId(apComment.getId().toHexString());
            apCommentDtos.add(apCommentDto);
        }
        return ResponseResult.okResult(apCommentDtos);
    }

    @Override
    public long like(CommentLikeDto dto) {

        ApComment apComment = mongoTemplate.findById(dto.getCommentId(),ApComment.class);
        Integer userId = AppThreadLocalUtil.get().getUserId();
        ApCommentLike apCommentLike = mongoTemplate.findOne(new Query(Criteria.where("authorId").is(userId).and("commentId").is(dto.getCommentId())), ApCommentLike.class);

        if(apCommentLike==null)
        {
            apCommentLike=new ApCommentLike();
        }
        BeanUtils.copyProperties(dto,apCommentLike);
        apCommentLike.setAuthorId(userId);
        apCommentLike.setCommentId(dto.getCommentId().toHexString());
        mongoTemplate.save(apCommentLike);
            long count = mongoTemplate.count(new Query(Criteria.where("commentId").is(dto.getCommentId()).and("operation").is(0)), ApCommentLike.class);
        return count;

    }
}
